/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.bizon.dinner.gui.topcompoments;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Logger;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.TableModel;

import org.bizon.dinner.gui.dialogs.AddItemDialog;
import org.bizon.dinner.gui.dialogs.SecuritySystem;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

import diiner.common.webaccess.UnAutorizationException_Exception;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.bizon.dinner.gui.topcompoments//Admin//EN", autostore = false)
public final class AdminTopComponent extends TopComponent {

	private static AdminTopComponent instance;
	/** path to the icon used by the component and its open action */
	// static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
	private static final String PREFERRED_ID = "AdminTopComponent1";

	public AdminTopComponent() {
		initComponents();
		setName(NbBundle.getMessage(AdminTopComponent.class, "CTL_AdminTopComponent"));
		setToolTipText(NbBundle.getMessage(AdminTopComponent.class, "HINT_AdminTopComponent"));
		// setIcon(ImageUtilities.loadImage(ICON_PATH, true));
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jScrollPane1 = new JScrollPane();
		jTable1 = new JTable();
		cancelButton = new JButton();
		okButton = new JButton();
		addButton = new JButton();

		jTable1.setModel(getTableModel());
		jScrollPane1.setViewportView(jTable1);

		cancelButton.setText(NbBundle.getMessage(AdminTopComponent.class, "AdminTopComponent.cancelButton.text")); // NOI18N
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});

		okButton.setText(NbBundle.getMessage(AdminTopComponent.class, "AdminTopComponent.okButton.text")); // NOI18N
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				okButtonActionPerformed(evt);
			}
		});

		addButton.setText(NbBundle.getMessage(AdminTopComponent.class, "AdminTopComponent.addButton.text")); // NOI18N
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				addButtonActionPerformed(evt);
			}
		});

		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(
				Alignment.TRAILING,
				layout.createSequentialGroup().addContainerGap(193, Short.MAX_VALUE).addComponent(addButton).addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(okButton).addPreferredGap(ComponentPlacement.RELATED).addComponent(cancelButton).addContainerGap()).addGroup(
				layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 375, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(15, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(
				Alignment.TRAILING,
				layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE).addPreferredGap(
						ComponentPlacement.RELATED).addGroup(
						layout.createParallelGroup(Alignment.BASELINE).addComponent(cancelButton).addComponent(okButton).addComponent(addButton))
						.addContainerGap()));
	}// </editor-fold>//GEN-END:initComponents

	private void addButtonActionPerformed(ActionEvent evt) {// GEN-FIRST:event_addButtonActionPerformed
		new AddItemDialog(null, true, model).setVisible(true);
	}// GEN-LAST:event_addButtonActionPerformed

	private void okButtonActionPerformed(ActionEvent evt) {// GEN-FIRST:event_okButtonActionPerformed
		try {
			SecuritySystem.getDinnerService().setMenu(SecuritySystem.getID(), model.items);
		} catch (UnAutorizationException_Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
		close();
	}// GEN-LAST:event_okButtonActionPerformed

	private void cancelButtonActionPerformed(ActionEvent evt) {// GEN-FIRST:event_cancelButtonActionPerformed
		close();
	}// GEN-LAST:event_cancelButtonActionPerformed

	AdminTableModel model = new AdminTableModel();

	private TableModel getTableModel() {
		return model;
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JButton addButton;
	private JButton cancelButton;
	private JScrollPane jScrollPane1;
	private JTable jTable1;
	private JButton okButton;

	// End of variables declaration//GEN-END:variables
	/**
	 * Gets default instance. Do not use directly: reserved for *.settings files
	 * only, i.e. deserialization routines; otherwise you could get a
	 * non-deserialized instance. To obtain the singleton instance, use
	 * {@link #findInstance}.
	 */
	public static synchronized AdminTopComponent getDefault() {
		if (instance == null) {
			instance = new AdminTopComponent();
		}
		return instance;
	}

	/**
	 * Obtain the AdminTopComponent instance. Never call {@link #getDefault}
	 * directly!
	 */
	public static synchronized AdminTopComponent findInstance() {
		TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
		if (win == null) {
			Logger.getLogger(AdminTopComponent.class.getName()).warning(
					"Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
			return getDefault();
		}
		if (win instanceof AdminTopComponent)
			return (AdminTopComponent) win;
		Logger.getLogger(AdminTopComponent.class.getName()).warning(
				"There seem to be multiple components with the '" + PREFERRED_ID + "' ID. That is a potential source of errors and unexpected behavior.");
		return getDefault();
	}

	@Override
	public int getPersistenceType() {
		return TopComponent.PERSISTENCE_NEVER;
	}

	@Override
	public void open() {
		if (SecuritySystem.isAdmin()) {
			super.open();
		}
	}

	@Override
	public void componentOpened() {
	}

	@Override
	public void componentClosed() {

	}

	void writeProperties(java.util.Properties p) {
	}

	Object readProperties(java.util.Properties p) {
		if (instance == null) {
			instance = this;
		}
		instance.readPropertiesImpl(p);
		return instance;
	}

	private void readPropertiesImpl(java.util.Properties p) {

	}

	@Override
	protected String preferredID() {
		return PREFERRED_ID;
	}
}
